#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "CH341DLL.H"
#include <QTimer>
#include <QDebug>
#include <QMessageBox>

// 初始化设备状态
ULONG MainWindow::deviceStatus = CH341_DEVICE_REMOVE;

/** @brief 设备状态回调函数
 *         CH341_DEVICE_ARRIVAL		3	设备插入事件,已经插入
 *         CH341_DEVICE_REMOVE_PEND	1	设备将要拔出
 *         CH341_DEVICE_REMOVE		0	设备拔出事件,已经拔出
*/
void CALLBACK deviceStatusCallback(ULONG iEventStatus){
    MainWindow::deviceStatus = iEventStatus;
    if(iEventStatus == CH341_DEVICE_ARRIVAL){
        qDebug() << "设备已经插入";
    }else if (iEventStatus == CH341_DEVICE_REMOVE_PEND){
        qDebug() << "设备将要拔出";
    }else if (iEventStatus == CH341_DEVICE_REMOVE){
        qDebug() << "设备已经拔出";
    }
}

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    oled.setDeviceAddress(0x3C);
    oled.OLED_Init();

    ULONG icVersion = CH341GetVerIC(0);
    qDebug()<< "芯片版本:" << icVersion;
    if(icVersion != 0 ){
        MainWindow::deviceStatus = CH341_DEVICE_ARRIVAL;
    }

    // 状态回调
    CH341SetDeviceNotify(0,NULL,deviceStatusCallback);

    // 初始化槽函数
    this->initSlots();

    // 定时器
    timer = new QTimer(this);
    timer->setInterval(100);        // 100ms
    timer->setSingleShot(false);    // 周期性触发
    connect(timer, SIGNAL(timeout()), this, SLOT(updateDeviceStatus()));
    timer->start();
}

/**
 * @brief 初始化信号和槽的连接
 */
void MainWindow::initSlots(){
    // 全屏显示 黑色
    connect(ui->btn_clear_black,&QPushButton::clicked,this,[=](){
          oled.OLED_Clear(0);
    });

    // 全屏显示 白色
    connect(ui->btn_clear_white,&QPushButton::clicked,this,[=](){
       oled.OLED_Clear(1);
    });

    // 在屏幕上画圆
    connect(ui->btn_draw_circle,&QPushButton::clicked,this,[=](){
        oled.OLED_NewFrame();
        oled.OLED_DrawCircle(64,32,16);
        oled.OLED_ShowFrame();
    });

    // 读取数据
    connect(ui->btn_read_data,&QPushButton::clicked,this,[=](){

    });

    // 发送数据
    connect(ui->btn_send_data,&QPushButton::clicked,this,[=](){
        UCHAR dataBuffer[100] = {0};
        dataBuffer[0] = 0x78;
        dataBuffer[1] = 0x00;
        int counter =2;
        QString  str = ui -> lineEdit->text();
        UCHAR ch = hexCharToInt(str[0].toLatin1())<<4;
        qDebug() << QString::number(ch, 16);
        ch = hexCharToInt(str[1].toLatin1());
        qDebug() << QString::number(str[1].toLatin1(), 16);
        qDebug() << QString::number(ch, 16);
//        if(str.length()%2!=0){
//            QMessageBox::information(nullptr, "提示", "请输入完整的数据,0x00！");
//        }else{
//            for(int i =0 ;i <str.length();i=i+2){
//                UCHAR result = 0;
//                unsigned char charValue = static_cast<unsigned char>(str.at(i).unicode());
//                result = hexCharToInt(charValue)*16;
//                qDebug()<<QString::number(result, 16);
//                result = str.at(i).digitValue() << 4;
//                qDebug()<<QString::number(result, 16);
//                charValue = static_cast<unsigned char>(str.at(i+1).unicode());
//                result = result + charValue;
////                result = hexCharToInt(charValue);
//                qDebug()<<QString::number(result, 16);
//                dataBuffer[(i/2)+2] = result;
//                counter++;
////              CH341WriteI2C(0,0x3C,0x00,result);
//            }
//            CH341StreamI2C(0,counter,dataBuffer,NULL,NULL);
//        }
//        qDebug() << str.toStdU16String();
//        qDebug() <<
//        UCHAR writeBuffer[100] = {0};
//        ui->te_send->te
//        CH341StreamI2C(0,);
    });
}

MainWindow::~MainWindow()
{
    CH341CloseDevice(0);
    delete timer;
    delete ui;
}

/**  定时器槽函数：
 *     更新设备状态到界面上
 */
void MainWindow::updateDeviceStatus(){
    switch(MainWindow::deviceStatus){
        case CH341_DEVICE_ARRIVAL:
            ui->label_status->setText("状态:设备已插入");
        break;
        case CH341_DEVICE_REMOVE_PEND:
            ui->label_status->setText("状态:设备即将拔出");
        break;
        case CH341_DEVICE_REMOVE:
            ui->label_status->setText("状态:设备已经拔出");
        break;
    }
}

char MainWindow::hexCharToInt(char ch){
    if(ch>='0'&&ch<=9){
        ch = ch -'0';
    }else if (ch >= 'A' && ch <= 'F'){
        ch = ch - 'A' +10;
    }else if (ch >= 'a' && ch <= 'f'){
        ch = ch - 'a' + 10;
    }
    return ch;
}






